﻿
namespace ImportData
{
    /// <summary>
    /// 导入样品结果时，使用的公共结构
    /// </summary>
    public class Result
    {
        public Result(string id, string[] analytesCode, string[] value)
        {
            AnalytesCode = analytesCode;
            ID = id;
            Value = value;
        }
        /// <summary>
        /// 样品ID
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// 分析项，一次导入多个分析项，分析项与结果一一对应
        /// </summary>
        public string[] AnalytesCode { get; set; }
        /// <summary>
        /// 结果
        /// </summary>
        public string[] Value { get; set; }
    }

    public static class Common
    {
        /// <summary>
        /// 读取 txt、csv 文件
        /// </summary>
        /// <param name="filePath">文件上传路径</param>
        /// <returns></returns>
        public static List<Result> ReadTxt(string filePath)
        {
            var result = new List<Result>();
            StreamReader sr = File.OpenText(filePath);
            string? nextLine;
            string[] Element = new string[1];
            int a = 1;
            while ((nextLine = sr.ReadLine()) != null)
            {
                if (a == 3)
                {
                    var row = nextLine.Split(';');
                    Element[0] = row[2].Split(' ')[0];
                }
                if (a >= 10)
                {
                    var row = nextLine.Split(';');
                    if (row[0].Length >= 10)
                    {
                        string[] Value = new string[1];
                        Value[0] = row[1];
                        var re = new Result(row[0], Element, Value);
                        result.Add(re);
                    }
                }
                a++;
            }
            sr.Close();

            return result;
        }

    }
}
